package com.example.ktapplication.utils

import android.content.Context
import android.net.ConnectivityManager
import com.example.ktapplication.enum.NetType

class NetworkUntil  {

    companion object {
        private var instance: NetworkUntil? = null

        fun getInstance(): NetworkUntil {
            if (instance == null) {
                instance = NetworkUntil()
            }
            return instance!!
        }
    }


    fun getNetworkType(context: Context): NetType {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected){
            var type:NetType
            when(activeNetworkInfo.type){
                ConnectivityManager.TYPE_WIFI -> type=NetType.WIFI
                ConnectivityManager.TYPE_MOBILE-> type=NetType.MOBILE
                else->type=NetType.NONE
            }
            return type
        }else{
            return NetType.NONE
        }
    }

}